<template>
	<view class="container">
		<view class="background-wrap">
			<image class="bg-image" src="/static/images/ic_mine_bg.png" mode="aspectFill" />
		</view>

		<image class="close-button" @tap="closePage" src="/static/images/ic_login_nav_close.png"
			:style="{marginTop: statusBarHeight + 'px'}"></image>

		<text class="title">密码登录</text>

		<view class="content-container">
			<view class="input-container">
				<watch-input v-model="phoneData" type="number" maxlength="11" placeholder="用户名/电话" class="custom-input"
                     :focus="true"></watch-input>
			</view>

			<view class="input-container">
				<watch-input v-model="passData" type="password" placeholder="密码" isShowPass="true"></watch-input>
			</view>

			<text class="forget-password" @tap="goToForgetPassword">找回密码</text>

			<view class="login-button" @click="login">
				登 录
			</view>

			<view class="view_private">
				<text @tap="onPrivacyPolicyChange" class="cuIcon" style="font-size: 14px;margin-right: 0"
					:class="agreePrivacyPolicy?'cuIcon-radiobox':'cuIcon-round'"> </text>
				同意
				<text @tap="gouto" data-url="https://file.huishouka.com.cn/html/1742962317970.html"
					class="private-link">《{{apptitle}}用户协议》</text>
				与
				<text @tap="gouto" data-url="https://file.huishouka.com.cn/html/1742963949398.html"
					class="private-link">《{{apptitle}}隐私协议》</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phoneData: '',
				passData: '',
				showPassword: false,
				agreePrivacyPolicy: false,
				apptitle: '',
				isFromNav: false,
				rightSafeArea: 0,
				statusBarHeight: 20
			};
		},
		created() {
			// 获取系统信息
			const systemInfo = uni.getSystemInfoSync()
			//获取状态栏高度
			this.statusBarHeight = systemInfo.statusBarHeight

			// 微信小程序适配
			// #ifdef MP-WEIXIN
			const menuRect = uni.getMenuButtonBoundingClientRect()
			this.rightSafeArea = systemInfo.windowWidth - menuRect.left // 计算右侧避让距离
			// #endif
		},
		onLoad(option) {
			this.apptitle = this.$app.title;
			if (option) {
				console.log(option);
				this.isFromNav = option.isNav === "true";
			}
		},
		methods: {
			closePage() {
				// 关闭页面逻辑
				if (this.isFromNav) {
					uni.navigateBack();
				} else {
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
			},
			togglePasswordVisibility() {
				this.showPassword = !this.showPassword;
			},
			login() {
				// 登录验证
				if (!this.phoneData) {
					uni.showToast({
						title: '手机号不能为空',
						position: 'bottom',
						icon: 'none'
					});
					return false;
				}

				const phoneReg = /^1[3-9]\d{9}$/;
				if (!phoneReg.test(this.phoneData)) {
					uni.showToast({
						title: '手机号格式错误',
						position: 'bottom',
						icon: 'none'
					});
					return false;
				}

				if (!this.passData) {
					uni.showToast({
						title: '密码不能为空',
						position: 'bottom',
						icon: 'none'
					});
					return false;
				}

				const pwdReg = /^(?=.*[a-zA-Z])(?=.*\d|.*[^a-zA-Z\d]).{6,20}$/;
				if (!pwdReg.test(this.passData)) {
					uni.showToast({
						title: '密码需包含字母+数字/符号，长度6-20位',
						position: 'bottom',
						icon: 'none'
					});
					return false;
				}

				if (!this.agreePrivacyPolicy) {
					uni.showToast({
						title: '请同意隐私政策',
						position: 'bottom',
						icon: 'none'
					});
					return false;
				}

				console.log('Login with:', this.phoneData, this.passData);
				this.requestLogin();
			},
			goToForgetPassword() {
				// 前往忘记密码页面逻辑
				console.log('Go to Forget Password Page');
				uni.navigateTo({
					url: '/pages/find-password/find-password'
				})
			},
			async requestLogin() {
				const res = await this.$http({
					method: 'POST',
					url: '/user/login',
					data: {
						mobile: this.phoneData,
						password: this.passData,
					},
				});

				if (res) {
					console.log('登录成功:', res, res.token, this.phoneData);
					this.$app.setLoginStorage(res.token);

					this.loadUserData();
				}
			},
			async loadUserData() {
				const res = await this.$http({
					method: 'GET',
					url: '/user/base/info',
				});

				if (res) {
					this.$app.setUserInfo(res.data);
					// 关闭页面逻辑
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
			},
			// 跳转到隐私
			gouto(e) {
				let url = e.target.dataset.url
				this.$app.openUrl(url, false)
			},
			onPrivacyPolicyChange() {
				// 更新 agreePrivacyPolicy 的值
				this.agreePrivacyPolicy = !this.agreePrivacyPolicy;
				console.log('Privacy Policy Agree Status:', this.agreePrivacyPolicy);
			},
			toAgreePriva() {
				uni.showToast({
					title: '请同意隐私政策',
					position: 'bottom',
					icon: 'none'
				});
			},
			async getPhoneNumber(e) {
				const openId = this.getIsActivationRegist()
				const bdId = this.getBdIsActivationRegist()
				const res = await this.$http({
					method: 'GET',
					url: '/user/wxMini/v2/mobile/login',
					data: {
						code: e.detail.code,
						openId: openId,
						bdVid: bdId
					},
				})

				if (res) {
					uni.setStorageSync('isActivationRegist', true);
					this.$app.setLoginStorage(res.token);
					this.loadUserData();
				}
			},
			// 是否上报微信
			getIsActivationRegist(){
				const isActiva = this.$app.isActivationRegist();
				return isActiva ? '' : uni.getStorageSync('activationData')
			},
			// 是否上报百度
			getBdIsActivationRegist(){
				const isActiva = this.$app.isActivationRegist();
				return isActiva ? '' : uni.getStorageSync('bdActivationData')
			}
		}
	};
</script>

<style lang="scss">
	.container {
		position: relative;
		width: 100vw;
		height: 100vh;
		display: flex;
		flex-direction: column;
	}

	/* 背景图容器 */
	.background-wrap {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 616rpx;
		/* 控制背景图高度为 Banner 的一半 */
		z-index: 0;
		/* 确保背景图在 Banner 下方 */

		.bg-image {
			width: 100%;
			height: 100%;
		}
	}

	.close-button {
		width: 68rpx;
		height: 68rpx;
		margin-left: 30rpx;
	}

	.title {
		z-index: 1;
		font-size: 44rpx;
		font-weight: bold;
		margin-bottom: 46rpx;
		margin-top: 100rpx;
		margin-left: 50rpx;
	}

	.content-container {
		background-color: white;
		border-radius: 36rpx;
		padding: 52rpx 32rpx;
		width: calc(100% - 84rpx);
		margin: 0 42rpx;
		z-index: 2 !important;
		/* 强制提升层级 */
	}

	.input-container {
		z-index: 1;
		width: 100%;
		background-color: #F2F9FF;
		border-radius: 20rpx;
		margin-bottom: 24rpx;
	}

	.forget-password {
		align-self: center;
		text-align: center;
		color: #4D7EF4;
		cursor: pointer;
		margin-top: 30rpx;
		font-size: 24rpx;
	}

	.login-button {
		margin-top: 120rpx;
		box-sizing: content-box;
		background-color: #4D7EF4;
		padding: 22rpx;
		text-align: center;
		font-size: 28rpx;
		color: white;
		border-radius: 8rpx;
	}

	.view_private {
		margin-top: 24rpx;
		font-size: 24rpx;
	}

	.private-link {
		color: #4D7EF4;
	}
</style>